package wqh.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import wqh.domain.Role;
import wqh.pojo.MapRet;
import wqh.service.RoleService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/water/user_role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @RequestMapping(value = "/getAll",method = RequestMethod.GET)
    public Object getAll()
    {
        try {
            List<Object> objectList = new ArrayList<Object>();
            List<Role> roleList = roleService.findThanRole(new Integer(1));
            if(roleList.size() == 0) return MapRet.SQL_ERROR();
            for(Role role : roleList){
                Map<String,Object> map = new HashMap<String, Object>();
                map.put("id",role.getId());
                map.put("text",role.getName());
                objectList.add(map);
            }
            return MapRet.DATA(objectList);
        }catch (Exception e){
            e.getMessage();
            return MapRet.SQL_ERROR();
        }
    }

}
